package com.example.star.service;

import com.example.star.entity.BuyNow;
import com.example.star.entity.Favorite;
import com.example.star.entity.Perimeter;
import com.example.star.entity.ShoppingCart;
import com.example.star.mapper.FavoriteMapper;
import com.example.star.mapper.PerimeterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FavoriteService {
    @Autowired
    private FavoriteMapper favoriteMapper;

    public void saveFavorite(Favorite favorite) {
        favoriteMapper.insertFavorite(favorite);
    }

    public List<Favorite> getAllFavorites() {
        return favoriteMapper.getAllFavorites();
    }

    public List<Favorite> getFavoriteByuserName(String userName){
        return favoriteMapper.getFavoriteByuserName(userName);
    }

    public List<Favorite> getFavoritesByPage(int page, int size) {
        int offset = (page - 1) * size;
        return favoriteMapper.getFavoritesByPage(offset, size);
    }

    public int getTotalFavorites() {
        return favoriteMapper.getTotalFavorites();
    }

    public void deleteFavoriteById(int id) {
        favoriteMapper.deleteFavoriteById(id);
    }
    public Favorite getFavoriteById(int id) {
        return favoriteMapper.getFavoriteById(id);
    }

    public Favorite getFavoriteByTitle(String title) {
        return favoriteMapper.getFavoriteByTitle(title);
    }

    public boolean isFavorite(String userName, String title) {
        int count = favoriteMapper.checkFavorite(userName, title);
        return count > 0;
    }

    public boolean deleteFavorite(String userName, String title) {
        int rowsAffected = favoriteMapper.deleteFavorite(userName, title);
        return rowsAffected > 0;
    }

    // 根据用户名分页获取订单信息
    public List<Favorite> getBuyNowsByUserName(String userName, int page, int size) {
        int offset = (page - 1) * size;
        return favoriteMapper.getFavoritesByUserNameAndPage(userName, offset, size);
    }

    // 根据用户名获取订单信息的总数
    public int getTotalBuyNowsByUserName(String userName) {
        return favoriteMapper.getTotalFavoritesByUserName(userName);
    }

}